﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using Engine;

namespace Game
{
    class Type95 : Weapon
    {
        int add;
        PlayerIndex index;
        Matrix[] bones;
        //Lo unico que en realidad cambia son los settings del constructor, las funciones
        //shoot y reload son iguales en todas las armas.
        public Type95()
        {
            mMaxBullets = 200;
            mBulletsPerClip = 10;
            mBulletsQuantity = 20;
            mBulletsLeft = mBulletsPerClip;
            mDamage = 60;
            mFireRate = 0.35f;
            mNextFireTime = 0;
            mReloadTime = 0;
            mAutoReload = false;
            mIsActiveWeapon = false;
            mWeaponName = "Type95";
            mLaserName = "type95Laser";
            add = 0;
            bones = new Matrix[ModelDrawer.Type95.Bones.Count];
            ModelDrawer.Type95.CopyAbsoluteBoneTransformsTo(bones);

            mBulletsFired = 0;
            mBulletList = new List<Bullet>();

            for (int i = 0; i < mBulletsPerClip; i++)
            {
                mBulletList.Add(new Bullet(5));
            }

        }
      
        public override void Update(GameTime gameTime)
        {
            if (mIsActiveWeapon)
            {
                #region Reload

                if ((Input.IsNewKeyPress(Keys.R) ||
                    Input.IsNewButtonPress(Buttons.X, JoysticksInfo.playerIndex, out mAuxIndex)) 
                    && !mAutoReload && mBulletsLeft != mBulletsPerClip)
                {
                    if (mBulletsQuantity > 0)
                    {
                        if (mBulletsQuantity >= mBulletsPerClip)
                        {
                            mBulletsQuantity -= mBulletsPerClip - mBulletsLeft;
                            mBulletsLeft = mBulletsPerClip;
                        }
                        else
                            if (mBulletsQuantity < mBulletsPerClip && mBulletsQuantity >= mBulletsPerClip - mBulletsLeft)
                            {

                                mBulletsQuantity -= BulletsPerClip - BulletsLeft;
                                BulletsLeft += BulletsPerClip - BulletsLeft;
                            }
                            else
                                if (mBulletsQuantity < mBulletsPerClip && mBulletsQuantity < mBulletsPerClip - mBulletsLeft)
                                {

                                    mBulletsLeft += mBulletsQuantity;
                                    mBulletsQuantity = 0;
                                }
                    }
                }

                if (mBulletsLeft == 0 && mBulletsQuantity > 0)
                {
                    mAutoReload = true;
                    Reload();
                }
                if (mAutoReload)
                {
                    mReloadTime += gameTime.ElapsedGameTime.Milliseconds;
                }

                #endregion


                if (mBulletsFired == mBulletList.Count)
                {
                    for (int i = 0; i < mBulletList.Count; i++)
                    {
                        mBulletList[i].Reset();
                    }
                    mBulletsFired = 0;

                }

                for (int i = 0; i < mBulletList.Count; i++)
                {
                    mBulletList[i].Update(gameTime);
                }

            }
        }
        public override void Shoot(Matrix playerWorld, Vector3 initialPos, Vector3 direction)
        {
            Matrix aux = bones[0] * playerWorld;
            Vector3 pos = Vector3.Zero;
            pos = Vector3.Transform(pos, aux);
            if (mBulletsFired <= mBulletList.Count && BulletsLeft > 0)
            {
                mBulletList[mBulletsFired].Fire(pos, direction);
                mBulletsFired++;
                mBulletsLeft--;
                AudioManager.PlaySound("pistola_fire");
            }
        }

        public override void Reload()
        {
            if (mReloadTime >= 2000)
            {
                if (mBulletsQuantity >= mBulletsPerClip)
                {
                    mBulletsQuantity -= mBulletsPerClip;
                    mBulletsLeft = mBulletsPerClip;
                    mAutoReload = false;
                    mReloadTime = 0;
                }
                else
                {
                    mBulletsLeft = mBulletsQuantity;
                    mBulletsQuantity -= mBulletsQuantity;
                    mAutoReload = false;
                    mReloadTime = 0;
                }
            }
        }

        public override void Draw(Matrix playerWorld, Matrix view, Matrix projection)
        {
            ModelDrawer.Draw(mWeaponName, playerWorld, view, projection);
            ModelDrawer.Draw(mLaserName, playerWorld, view, projection);

            for (int i = 0; i < mBulletList.Count; i++)
            {
                if (mBulletList[i].wasFired)
                {
                    mBulletList[i].Draw(view, projection);
                }
            }
        }
    }
}
